class Manage::PermissionsController < ApplicationController
  include Security::Session::Access
  include Utils::Url
  include AccessGroups

  def list_system_objects
    @system_objects = SystemObject.find_managed
  end

  def show_system_object
    @system_object = SystemObject.find(id_param)
  end

  def list_roles
    @roles = Role.find(:all, :order => "id")
  end

  def show_role
    @role = Role.find(id_param)
  end

  def update_role
    case request.method
    when :post
      if params[:commit] == "Cancel"
        redirect_to(:action => :list_roles)
        return
      end
      @role = Role.find(params[:role][:id])
      params[:role].delete(:id)
      @role.attributes=(params[:role])
      if save_role?
        flash[:notice] = "Role successfully updated"
        redirect_to(:action => "show_role", :id => @role.id)
        return
      end 
    else
      @role = Role.find(id_param)
    end
  end

  def list_user_groups
    @user_groups = UserGroup.find(:all)
  end

  def create_user_group
    case request.method
    when :post
      params[:user_group].delete(:id)
      @user_group = UserGroup.new(params[:user_group])
      if save_user_group?
        flash[:notice] = "User Group successfully created"
        redirect_to(:action => "show_user_group", :id => @user_group.id)
        return
      end 
    else
      @user_group = UserGroup.new
    end
    init_user_group_associations
  end

  def show_user_group
    @user_group = UserGroup.find(id_param)
  end

  def update_user_group
    case request.method
    when :post
      if params[:commit] == "Cancel"
        redirect_to(:action => :list_user_groups)
        return
      end
      @user_group = UserGroup.find(params[:user_group][:id])
      params[:user_group].delete(:id)
      @user_group.attributes=(params[:user_group])
      if save_user_group?
        flash[:notice] = "User Group successfully updated"
        redirect_to(:action => "show_user_group", :id => @user_group.id)
        return
      end 
    else
      @user_group = UserGroup.find(id_param)
    end
    init_user_group_associations
  end

  def delete_user_group
    @user_group = UserGroup.find(id_param)
    @user_group.destroy
    redirect_to(:action => "list_user_groups")
  end

  def list_editor_groups
    @editor_groups = EditorGroup.find(:all)
  end

  def create_editor_group
    case request.method
    when :post
      params[:editor_group].delete(:id)
      @editor_group = EditorGroup.new(params[:editor_group])
      if save_editor_group?
        flash[:notice] = "Editor Group successfully created"
        redirect_to(:action => "show_editor_group", :id => @editor_group.id)
        return
      end 
    else
      @editor_group = EditorGroup.new
    end
    init_editor_group_associations
  end

  def show_editor_group
    @editor_group = EditorGroup.find(id_param)
  end

  def update_editor_group
    case request.method
    when :post
      if params[:commit] == "Cancel"
        redirect_to(:action => :list_editor_groups)
        return
      end
      @editor_group = EditorGroup.find(params[:editor_group][:id])
      params[:editor_group].delete(:id)
      @editor_group.attributes=(params[:editor_group])
      if save_editor_group?
        flash[:notice] = "Editor Group successfully updated"
        redirect_to(:action => "show_editor_group", :id => @editor_group.id)
        return
      end 
    else
      @editor_group = EditorGroup.find(id_param)
    end
    init_editor_group_associations
  end


  def delete_editor_group
    @editor_group = EditorGroup.find(id_param)
    @editor_group.destroy
    redirect_to(:action => "list_editor_groups")
  end

  private
    def save_role?
      if @role.save
        add_role_standard_system_objects 
        update_role_associations
        return true
      end
      return false
    end

    def add_role_standard_system_objects
      sysobjects = SystemObject.find_managed
      sysobjects.each do |sysobj|
        unless @role.has_system_object_id?(sysobj.id)
          @role.system_object << sysobj
        end
      end
    end

    def update_role_associations
      #Clear orphans
      system_objects = @role.system_object.dup
      system_objects.each do |system_object|
        next unless system_object.can_remove 
        @role.system_object.delete(system_object) unless param?("system_object_#{system_object.id}")
      end
      #Add new
      params.each do |key,value|
        if key =~ /^system_object_/
          unless @role.has_system_object_id?(get_id_from_param(key))
            @role.system_object << SystemObject.find(get_id_from_param(key)) 
          end
        end
      end
    end

    def save_user_group?
      #@user_group.role_id = current_user_role_id unless current_user_is_site_admin?
      @user_group.role_id = SITE_USER
      if @user_group.save
        add_user_group_standard_system_objects
        update_user_group_associations
        return true
      end
      return false
    end

    def add_user_group_standard_system_objects
      sysobjects = SystemObject.find_managed
      sysobjects.each do |sysobj|
        unless @user_group.has_system_object_id?(sysobj.id)
          @user_group.system_object << sysobj
        end
      end
    end

    def init_user_group_associations
      init_group_users
      init_group_system_objects
      init_group_pages
      init_group_document_categories
      init_group_folders
    end

    def update_user_group_associations
      update_group_users(@user_group)
      update_group_system_objects(@user_group)
      update_group_pages(@user_group)
      update_group_document_categories(@user_group)
      update_group_folders(@user_group)
    end

    def save_editor_group?
      if @editor_group.save
        update_editor_group_associations
        return true
      end
      return false
    end

    def init_editor_group_associations
      init_group_users
      init_group_pages
      init_group_document_categories
      init_group_folders
    end

    def update_editor_group_associations
      update_group_users(@editor_group)
      update_group_pages(@editor_group)
      update_group_document_categories(@editor_group)
      update_group_folders(@editor_group)
    end

end
